//////////////////////////////////////////////////
//	CONTRAPTION CORES FOR GCOMBAT09		//
//////////////////////////////////////////////////

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

function ENT:Initialize()
	self.Entity:SetModel( "models/Slyfo/rover1_battery.mdl" )
	self.Entity:SetName("GCOMBAT Core")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	local phys = self.Entity:GetPhysicsObject()
	self.IsCore = true
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableCollisions(true)
		phys:EnableDrag(true)
	end
	gcombat.registerent(self.Entity)
	self.Outputs = WireLib.CreateOutputs(self.Entity, {"Health", "Armor", "Shields"})
	self.Inputs = WireLib.CreateInputs(self.Entity, {"Build Core", "!Self Destruct!"})
	WireLib.TriggerOutput(self.Entity, "Health", self.cbt.health)
end

function ENT:TriggerInput(iname, value)
	if iname == "Build Core" then
		self:RunLink()
	end
end

function ENT:RunLink()
	local ents = constraint.GetAllConstrainedEntities( self.Entity )
	self.cbt.health = self:GetPhysicsObject():GetMass() * 6
	for _,ent in pairs(ents) do
		local valid = gcombat.validate( constr.Entity )
		if not valid then
			gcombat.registerent( constr.Entity )
		end
		print("Is this getting run?")
		if valid then
			print("I seriously doubt this is...")
			self.cbt.health = constr.Entity.cbt.health + self.cbt.health
			self.cbt.armor = constr.Entity.cbt.armor + self.cbt.armor
			constr.Entity.cbt.core = self
		end
		print("We didn't stop?!")
	end
end

function ENT:Think()
	WireLib.TriggerOutput(self.Entity, "Health", self.cbt.health)
	WireLib.TriggerOutput(self.Entity, "Armor", self.cbt.armor)
end
